#!/bin/bash

timeout=1200
pull=false
while getopts "t:hp" opt;
do
  case $opt in
    t)
      timeout=$OPTARG
      ;;
    p)
      pull=true
      ;;
    h)
      echo "Usage:"
      echo "$0 [-t timeout] image-name container-name [options for container]"
      echo "\t\t timeout: wait for timeout seconds, default 900"
      exit 0
      ;;
  esac
done

shift $((OPTIND-1))
image=$1
name=$2
shift 2
#echo $@
if [ "${pull}" == "true" ]; then docker pull $image; fi

# clean up any old instances

docker rm -f $name > /dev/null 2>&1

set -e
docker run -d --privileged --name $name $image --trace $@
SECONDS=0
last_uptime=0
echo "Waiting for $name to become healthy"
set +e
while [ $SECONDS -lt $timeout -a "$health" != "healthy" -a "$status" != "exited" ]
do
  sleep 2
  echo -n "."
  health=$(docker inspect --format '{{.State.Health.Status}}' $name)
  if [ $? -ne 0 ]; then exit 1; fi
  if [ $(( SECONDS - last_uptime )) -ge 120 ]
  then
    echo "$name is $health after $SECONDS seconds"
    last_uptime=$SECONDS
  fi
  status=$(docker inspect --format '{{.State.Status}}' $name)
done

echo "\n"
if [ $health = "healthy" ]
then
  echo -e "\e[32m$name became healthy in $SECONDS seconds\e[0m"
  container_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' "$name")

  if [ -z "$container_ip" ]; then
    echo "Error: Unable to retrieve container IP address."
    exit 1
  fi

  echo "Container IP address: $container_ip"

  ssh_user="vrnetlab"
  ssh_password="VR-netlab9"

  # Test SSH connection using sshpass. We run the "exit" command, but since not
  # all platforms support it, we instead check whether an interactive session
  # was started by checking the output for the string "Entering interactive session"
  sshpass -v -p "$ssh_password" ssh -v -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$ssh_user@$container_ip" exit 2>&1 | rg --passthru "Entering interactive session"

  if [ $? -eq 0 ]; then
    echo "SSH connection test passed."
  else
    echo "SSH connection test failed."
    exit 1
  fi
  docker stop $name
else
  echo -e "\e[31m$name failed to become healthy after $SECONDS seconds\e[0m"
  # leave the container running for local troubleshooting
  false
fi
